﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Heyo.Class.Animations
{
    public class MatrixAnimation : MatrixAnimationBase
    {

        public static readonly DependencyProperty FromProperty = DependencyProperty.Register(
            "From", typeof(Matrix), typeof(MatrixAnimation), new PropertyMetadata(new Matrix(1,1,1,1,0,0)));

        public Matrix From
        {
            get => (Matrix) GetValue(FromProperty);
            set => SetValue(FromProperty, value);
        }

        public static readonly DependencyProperty ToProperty = DependencyProperty.Register(
            "To", typeof(Matrix), typeof(MatrixAnimation), new PropertyMetadata(default(Matrix)));

        public Matrix To
        {
            get => (Matrix) GetValue(ToProperty);
            set => SetValue(ToProperty, value);
        }

        public static readonly DependencyProperty EasingFunctionProperty = DependencyProperty.Register(
            "EasingFunction", typeof(IEasingFunction), typeof(MatrixAnimation), new PropertyMetadata(default(IEasingFunction)));

        public IEasingFunction EasingFunction
        {
            get => (IEasingFunction) GetValue(EasingFunctionProperty);
            set => SetValue(EasingFunctionProperty, value);
        }

        protected override Freezable CreateInstanceCore()
        {
            return new MatrixAnimation();
        }

        protected override Matrix GetCurrentValueCore(Matrix defaultOrigin, Matrix defaultToOrigin, AnimationClock animationClock)
        {
            if (animationClock.CurrentProgress == null) return From;
            double progress = EasingFunction?.Ease(animationClock.CurrentProgress.Value) ?? animationClock.CurrentProgress.Value;

            Matrix currentMatrix = new Matrix
            {
                M11 = From.M11 + (To.M11 - From.M11) * progress,
                M12 = From.M12 + (To.M12 - From.M12) * progress,
                M21 = From.M21 + (To.M21 - From.M21) * progress,
                M22 = From.M22 + (To.M22 - From.M22) * progress,
                OffsetX = From.OffsetX + (To.OffsetX - From.OffsetX) * progress,
                OffsetY = From.OffsetY + (To.OffsetY - From.OffsetY) * progress,
            };
            return currentMatrix;
        }
    }
}
